// 获取模拟数据
const mock = require('../mock');

module.exports = function handleApi (req) {
  let apiName = req.request.body.apiName;
  if (apiName && apis[apiName]) {
    return apis[apiName](req.request.body)
  } else {
    throw Error(`[错误] { apiName: '${apiName}' }, 找不到此 apiName`);
  }
}
  

const apis = {
  abc () {
    return {
      a: 1,
      b: 2,
    }
  },

  schemeAttrList (data) {
    return mock.schemeAttrList;
  },
  getDictList () {
    return mock;
  },

  getTemplateList (reqData) {
    let list = mock.templateList.data.content;
    let page = reqData.params.page;
    let pageSize = reqData.params.pageSize;

    let data = list.slice((page-1)*pageSize, page*pageSize);

    let total = list.length;
    let totalPages = (total % pageSize) > 0 ? parseInt(total / pageSize) + 1 : parseInt(total / pageSize);

    return JSON.stringify({
      data,
      pagination: {
        page,
        pageSize,
        total,
        totalPages,
      }
    })
  }
}